﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace MCEMediaManager.Model
{
    public class Episode
    {
        private int _ID;
        private int _TvdbID;
        private int _SeriesID;
        private int _SeriesTvdbID;
        private string _Name;
        private int _EpisodeNumber;
        private int _SeasonNumber;
        private DateTime _OriginalAirDate;
        private string _Overview;
        private decimal _Rating;
        private int _SeasonTvdbID;
        private Series _SeriesInfo;

        #region Public Properties

        public int ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }

        public int TvdbID
        {
            get
            {
                return _TvdbID;
            }
            set
            {
                _TvdbID = value;
            }
        }

        public int SeriesID
        {
            get
            {
                return _SeriesID;
            }
            set
            {
                _SeriesID = value;
                SeriesInfo.ID = value;
            }
        }

        public int SeriesTvdbID
        {
            get
            {
                return _SeriesTvdbID;
            }
            set
            {
                _SeriesTvdbID = value;
                SeriesInfo.TvdbID = value;
            }
        }

        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }

        public int EpisodeNumber
        {
            get
            {
                return _EpisodeNumber;
            }
            set
            {
                _EpisodeNumber = value;
            }
        }

        public int SeasonNumber
        {
            get
            {
                return _SeasonNumber;
            }
            set
            {
                _SeasonNumber = value;
            }
        }

        public DateTime OriginalAirDate
        {
            get
            {
                return _OriginalAirDate;
            }
            set
            {
                _OriginalAirDate = value;
            }
        }

        public string Overview
        {
            get
            {
                return _Overview;
            }
            set
            {
                _Overview = value;
            }
        }

        public decimal Rating
        {
            get
            {
                return _Rating;
            }
            set
            {
                _Rating = value;
            }
        }

        public int SeasonTvdbID
        {
            get
            {
                return _SeasonTvdbID;
            }
            set
            {
                _SeasonTvdbID = value;
            }
        }
        public Series SeriesInfo
        {
            get
            {
                if (_SeriesInfo == null)
                    _SeriesInfo = new Series();
                return _SeriesInfo;
            }
            set
            {
                _SeriesInfo = value;
            }
        }
        #endregion

        public void LoadFromDataReader(IDataReader dr)
        {
            PropertyInfo[] props = this.GetType().GetProperties();
            foreach (PropertyInfo prop in props)
            {
                switch (prop.Name)
                {
                    case "SeriesInfo":
                        break;
                    default:
                        try
                        {
                            prop.SetValue(this, dr[prop.Name], null);
                        }
                        catch
                        {
                        }
                        break;
                }
            }

        }

        public void LoadFromTvdbEpisode(TvdbLib.Data.TvdbEpisode episode)
        {
            this.TvdbID = episode.Id;
            this.Name = episode.EpisodeName;
            this.EpisodeNumber = episode.EpisodeNumber;
            this.OriginalAirDate = episode.FirstAired;
            this.Overview = episode.Overview;
            this.SeasonNumber = episode.SeasonNumber;
            this.SeriesTvdbID = episode.SeriesId;
        }


    }
}
